package sims.gui.salesperson;

import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import sims.entity.Bill;
import sims.entity.BillDetail;
import sims.entity.Item;
import sims.execution.Executor;

public class ReturnBill extends javax.swing.JPanel {

    /**
     * Creates new form ReturnBill
     */
    DefaultTableModel modelview = new DefaultTableModel() {
        @Override
        public boolean isCellEditable(int i, int i1) {
            return false;
        }
    };
    DefaultTableModel modelReturn = new DefaultTableModel() {
        @Override
        public boolean isCellEditable(int i, int i1) {
            if (i1 == 5) {
                return true;
            }
            return false;
        }
    };

    public ReturnBill() {
        initComponents();
    }
    String userName;
    int billId;
    ArrayList<BillDetail> arrBillDetail = new ArrayList<BillDetail>();

    public ReturnBill(int billId, String userName) {
        this.userName = userName;
        this.billId = billId;
        initComponents();
        Executor fun = new Executor();
        Bill bill = new Bill();
        String[] column = {"STT", "ItemID", "Item Name", "Unit", "Price", "Amount", "Cost"};
        modelview.setColumnIdentifiers(column);
        modelReturn.setColumnIdentifiers(column);
        jTable_OrderReturn.setModel(modelReturn);
        /* cua order view */

        bill = fun.getBillbyID(billId);
        arrBillDetail = fun.showBillDetailbyID(billId);
        lbDatetime.setText(bill.getDatetime().toString());
        String username = fun.getUserNamebyID(bill.getUserId());
        lbUserName.setText(username);
        lbTotal.setText(String.valueOf(bill.getTotal()));

        /* cua order return */

        Calendar cal = Calendar.getInstance();
        lbUserName1.setText(userName);
        lbDay1.setText(String.valueOf(cal.get(Calendar.DATE)));
        lbMonth1.setText(String.valueOf(cal.get(Calendar.MONTH) + 1));
        lbYear1.setText(String.valueOf(cal.get(Calendar.YEAR)));
        int stt = 0;
        for (BillDetail bd : arrBillDetail) {
            stt++;
            Item item = new Item();
            item = fun.getItemById(bd.getItemID());
            Object[] row = {stt, item.getId(), item.getItemName(), item.getUnit(), bd.getPriceSell(), bd.getAmount(), (bd.getPriceSell() * bd.getAmount())};
            modelview.addRow(row);
        }
        jTable_OrderSell.setModel(modelview);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnOrderReturn = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        lbUserName = new javax.swing.JLabel();
        lbTotal = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        lbDatetime = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_OrderSell = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lbTotal1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        txtItemName = new javax.swing.JTextField();
        bn_del = new javax.swing.JButton();
        bn_Ok = new javax.swing.JButton();
        txtAmount = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        bnSubmit = new javax.swing.JButton();
        lbYear1 = new javax.swing.JLabel();
        lbMonth1 = new javax.swing.JLabel();
        lbDay1 = new javax.swing.JLabel();
        lbUserName1 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable_OrderReturn = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnReturn = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        pnOrderReturn.setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbUserName.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbTotal.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel8.setText("Total:");

        lbDatetime.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jTable_OrderSell.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable_OrderSell);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("User Name:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Datetime :");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText(" Sell Order ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 34, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(lbDatetime, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lbUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(161, 161, 161)))
                .addGap(19, 19, 19))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addComponent(lbTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(lbDatetime, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lbUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                        .addGap(37, 37, 37)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(lbTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbTotal1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        bn_del.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        bn_del.setText("Delete");
        bn_del.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bn_delActionPerformed(evt);
            }
        });

        bn_Ok.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        bn_Ok.setText("OK");
        bn_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bn_OkActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel7.setText("Amount:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setText("ItemName:");

        bnSubmit.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        bnSubmit.setText("Submit");
        bnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bnSubmitActionPerformed(evt);
            }
        });

        lbYear1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbMonth1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbDay1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lbUserName1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel12.setText("Total:");

        jLabel11.setText("-");

        jLabel10.setText("-");

        jTable_OrderReturn.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(jTable_OrderReturn);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel9.setText("User Name :");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setText("Datetime :");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel4.setText("  Return Order");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(bnSubmit)
                        .addGap(35, 35, 35)
                        .addComponent(jButton1)
                        .addGap(176, 176, 176))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbTotal1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(14, 14, 14))))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(12, 12, 12)
                        .addComponent(txtItemName, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bn_Ok)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bn_del))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbUserName1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(lbDay1, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGap(14, 14, 14)
                                        .addComponent(lbMonth1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(31, 31, 31)
                                        .addComponent(lbYear1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(35, 35, 35)
                                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(0, 17, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                    .addGap(20, 20, 20)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(14, 14, 14)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lbYear1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbMonth1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbDay1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbUserName1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(10, 10, 10)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtItemName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bn_Ok)
                    .addComponent(bn_del)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 307, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12)
                    .addComponent(lbTotal1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(bnSubmit))
                .addGap(19, 19, 19))
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                    .addContainerGap(166, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(79, 79, 79)))
        );

        btnReturn.setText("---->");
        btnReturn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReturnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnOrderReturnLayout = new javax.swing.GroupLayout(pnOrderReturn);
        pnOrderReturn.setLayout(pnOrderReturnLayout);
        pnOrderReturnLayout.setHorizontalGroup(
            pnOrderReturnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnOrderReturnLayout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnReturn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnOrderReturnLayout.setVerticalGroup(
            pnOrderReturnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnOrderReturnLayout.createSequentialGroup()
                .addGroup(pnOrderReturnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnOrderReturnLayout.createSequentialGroup()
                        .addGap(245, 245, 245)
                        .addComponent(btnReturn))
                    .addGroup(pnOrderReturnLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnOrderReturnLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        add(pnOrderReturn, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    int stt = 0;
    private void bnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bnSubmitActionPerformed
        Executor exec = new Executor();
        ArrayList<String> arrUpdateItem = new ArrayList<String>();
        ArrayList<String> arrInsertOrder = new ArrayList<String>();
        ArrayList<String> arrInsertOrder_Item = new ArrayList<String>();
        String day = lbDay1.getText();
        String month = lbMonth1.getText();
        String year = lbYear1.getText();
        //String userId = lbUserName1.getText();
        int userId = exec.getUserIDbyName(userName);
        float total = 0;
        for (int i = 0; i < jTable_OrderReturn.getRowCount(); i++) {
            total += Float.parseFloat(jTable_OrderReturn.getValueAt(i, 6).toString());

            int itemId = Integer.parseInt(jTable_OrderReturn.getValueAt(i, 1).toString());
            for (BillDetail od : arrBillDetail) {
                if (itemId == od.getItemID()) {
                    arrBillDetail.remove(od);
                    break;
                }
            }
        }
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        String datetime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + "";

        if (jTable_OrderReturn.getRowCount() > 0) {
            int Submit = JOptionPane.showConfirmDialog(this, "Are you sure want submit ?", null, JOptionPane.YES_NO_OPTION);
            if (Submit == JOptionPane.OK_OPTION) {
                String insertOrder = "insert into orders values ('" + datetime + "','" + userId + "','returned','successed'," + total + ")";
                arrInsertOrder.add(insertOrder);
                exec.runBatch(arrInsertOrder);
                int billId = exec.getBillIDbyUserID(userId);
                for (int i = 0; i < jTable_OrderReturn.getRowCount(); i++) {
                    String itemId = jTable_OrderReturn.getValueAt(i, 1).toString();
                    int Amount = Integer.parseInt(jTable_OrderReturn.getValueAt(i, 5).toString());
                    float price = Float.parseFloat(jTable_OrderReturn.getValueAt(i, 4).toString());
                    String insertOrderItem = "insert into order_item values(" + billId + ",'" + itemId + "'," + Amount + "," + price + ")";
                    String updateItem = "update item set quantityAvailable +=" + Amount + " where itemId = " + itemId + " ";
                    arrUpdateItem.add(updateItem);
                    arrInsertOrder_Item.add(insertOrderItem);
                }
                exec.runBatch(arrUpdateItem);
                exec.runBatch(arrInsertOrder_Item);
                ArrayList<String> arrupdate = new ArrayList<String>();
                String update = "update orders set status ='returned'where orderId =" + this.billId + "";
                arrupdate.add(update);
                exec.runBatch(arrupdate);
                JOptionPane.showMessageDialog(this, "Submit successfull");
                this.removeAll();
                ListSellOrder lst = new ListSellOrder(userName);
                this.add(lst);
                this.validate();
            }

            //xoa het cac row trong bang
//            int rows = modelReturn.getRowCount();
//            for (int i = rows - 1; i >= 0; i--) {
//                modelReturn.removeRow(i);
//            }
//            lbTotal1.setText("");
//            
        } else {
            JOptionPane.showMessageDialog(this, "don't have Item returned!!!");

        }

    }//GEN-LAST:event_bnSubmitActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (jTable_OrderReturn.getRowCount() > 0) {
            int Submit = JOptionPane.showConfirmDialog(this, "are you sure want cancel ?", null, JOptionPane.YES_NO_OPTION);
            if (Submit == JOptionPane.OK_OPTION) {
                this.removeAll();
                ListSellOrder lst = new ListSellOrder(userName);
                this.add(lst);
                this.validate();
            }
        }else{
                this.removeAll();
                ListSellOrder lst = new ListSellOrder(userName);
                this.add(lst);
                this.validate();
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnReturnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReturnActionPerformed
        if (jTable_OrderSell.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "please select 1 item return");
        } else {
            int x = jTable_OrderSell.getSelectedRow();
            txtItemName.setText(jTable_OrderSell.getValueAt(x, 2).toString());
            }
        this.repaint();
    }//GEN-LAST:event_btnReturnActionPerformed

    private void bn_delActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bn_delActionPerformed
        if (jTable_OrderReturn.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "select item need delete");
        } else {
            stt--;
            int x = jTable_OrderReturn.getSelectedRow();
            modelReturn.removeRow(x);
            float total = 0;
            for (int i = 0; i < jTable_OrderReturn.getRowCount(); i++) {
                total += Float.parseFloat(jTable_OrderReturn.getValueAt(i, 6).toString());
            }
            lbTotal1.setText(String.valueOf(total));
        }
        this.repaint();
    }//GEN-LAST:event_bn_delActionPerformed

    private void bn_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bn_OkActionPerformed
        if ("".equals(txtItemName.getText()) || "".equals(txtAmount.getText())) {
            JOptionPane.showMessageDialog(this, "Item Name and Amount must not null!!!");
        } else if (!txtAmount.getText().matches("[1-9][0-9]*")) {
            JOptionPane.showMessageDialog(this, "Amount must is number!!!");
        } else {
            boolean checkIdtrung = true;
            for (int i = 0; i < jTable_OrderReturn.getRowCount(); i++) {
                if (txtItemName.getText().equals(jTable_OrderReturn.getValueAt(i, 2).toString())) {
                    checkIdtrung = false;
                    JOptionPane.showMessageDialog(this, "Item existed");
                    break;
                }
            }
            if (checkIdtrung) {
                Executor fun = new Executor();
                boolean flag = true;
                int amount = Integer.parseInt(txtAmount.getText());
                for (BillDetail od : arrBillDetail) {
                    if (txtItemName.getText().equals(od.getItemName())) {
                        flag = false;
                        if (amount > 0 && amount <= od.getAmount()) {
                            stt++;
                            Item pro = new Item();
                            pro = fun.getItemById(od.getItemID());
                            Object[] row = {stt, od.getItemID(), pro.getItemName(), pro.getUnit(), od.getPriceSell(), amount, (od.getPriceSell() * amount)};
                            modelReturn.addRow(row);
                            jTable_OrderReturn.setModel(modelReturn);
                            txtAmount.setText("");
                            txtItemName.setText("");
                            float total = 0;
                            for (int i = 0; i < jTable_OrderReturn.getRowCount(); i++) {
                                total += Float.parseFloat(jTable_OrderReturn.getValueAt(i, 6).toString());
                            }
                            lbTotal1.setText(String.valueOf(total));
                            break;
                        }
                        if (amount < 0) {
                            JOptionPane.showMessageDialog(this, "error: amount < 0");
                        }
                        if (amount > od.getAmount()) {
                            JOptionPane.showMessageDialog(this, "error: amount > amount purchased");
                        }
                    }
                }
                if (flag) {
                    JOptionPane.showMessageDialog(this, "Item Name don't have in sell order ");
                }
            }
        }
    }//GEN-LAST:event_bn_OkActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bnSubmit;
    private javax.swing.JButton bn_Ok;
    private javax.swing.JButton bn_del;
    private javax.swing.JButton btnReturn;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable_OrderReturn;
    private javax.swing.JTable jTable_OrderSell;
    private javax.swing.JLabel lbDatetime;
    private javax.swing.JLabel lbDay1;
    private javax.swing.JLabel lbMonth1;
    private javax.swing.JLabel lbTotal;
    private javax.swing.JLabel lbTotal1;
    private javax.swing.JLabel lbUserName;
    private javax.swing.JLabel lbUserName1;
    private javax.swing.JLabel lbYear1;
    private javax.swing.JPanel pnOrderReturn;
    private javax.swing.JTextField txtAmount;
    private javax.swing.JTextField txtItemName;
    // End of variables declaration//GEN-END:variables
}
